﻿/// <reference path="turbo_common.js" />
/// <reference path="../General/Json.js" />
/// <reference path="ResponseControl.js" />
/// <reference path="ResponseValidator.js" />
/// <reference path="ResponseUpdatePanel.js" />

var $AjaxResponse = TA.Turbo.Common.AjaxResponse = function AjaxResponse()
{
}
$AjaxResponse.Inherit(BaseJsonDeserializable);

if (_____a)
{
	$AjaxResponse.prototype = new BaseJsonDeserializable();
}
$AjaxResponse.prototype.ViewState = null;
$AjaxResponse.prototype.Controls = null;
$AjaxResponse.prototype.Validators = null;
$AjaxResponse.prototype.Main = null;
$AjaxResponse.prototype.Custom = null;
$AjaxResponse.prototype.Hash = null;
$AjaxResponse.prototype.UpdatePanels = null;
$AjaxResponse.prototype.Scenarios = null;
$AjaxResponse.prototype.Scripts = null;

$AjaxResponse.prototype.DeserializeContent = function DeserializeContent(prShortName, value)
{
	switch (prShortName)
	{
		case "E": this.ViewState = value; break;
		case "O":
			this.Controls = new Array();
			for (var i = 0; i < value.length; ++i)
			{
				var t = new $ResponseControl();
				t.Deserialize(value[i]);
				this.Controls.push(t);
			}
			break;
		case "V":
			this.Validators = new Array();
			for (var i = 0; i < value.length; ++i)
			{
				var t = new $ResponseValidator();
				t.Deserialize(value[i]);
				this.Validators.push(t);
			}
			break;
		case "M": this.Main = value; break;
		case "C": this.Custom = value; break;
		case "H": this.Hash = value; break;
		case "U":
			this.UpdatePanels = new Array();
			for (var i = 0; i < value.length; ++i)
			{
				var t = new $ResponseUpdatePanel();
				t.Deserialize(value[i]);
				this.UpdatePanels.push(t);
			}
			break;
		case "S": this.Scenarios = value; break;
		case "R": this.Scripts = value; break;
	}
}
$AjaxResponse.prototype.Process = function Process(scenario)
{
	if (this.ViewState != null)
	{
		$("__VIEWSTATE").value = this.ViewState;
	}

	scenario.RaiseEvent("Response", this);

	if (this.Validators != null)
	{
		for (var iVal = 0; iVal < this.Validators.length; ++iVal)
		{
			this.Validators[iVal].Process();
		}
	}
	else
	{
		if (this.Hash != null)
		{
			// if we have a response hash string, then set location; 
			// otherwise, set locaiton to "#" (not "") because "" will cause a refresh. if location empty, don't do anything.
			if (this.Hash != null && this.Hash != ""
					&& location.hash != null && location.hash != "")
				location.hash = "#" + this.Hash;
		}

		if (this.Controls != null)
		{
			for (var i = 0; i < this.Controls.length; ++i)
			{
				this.Controls[i].Process();
			}
		}
	}

	if (this.UpdatePanels != null)
	{
		for (var iUP = 0; iUP < this.UpdatePanels.length; ++iUP)
		{
			this.UpdatePanels[iUP].Process();
		}
	}

	if (this.Scenarios != null)
	{
		for (var i = 0; i < this.Scenarios.length; ++i)
		{
			var ps = new $PostScenario();
			ps.Deserialize(this.Scenarios[i]);
			TA.Turbo.Common.PostScenarios[ps.Id] = ps;
		}
	}

	if (this.Scripts != null)
	{
		for (var iScript = 0; iScript < this.Scripts.length; ++iScript)
		{
			eval(this.Scripts[iScript]);
		}
	}

	scenario.RaiseEvent("ResponseProcessed", this);
}
